package com.vegetable.modules.service.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.modules.entity.finance.BankFlowImportConfig;
import com.vegetable.modules.entity.finance.po.BankFlowImportConfigQueryPO;
import com.vegetable.modules.mapper.finance.BankFlowImportConfigMapper;
import com.vegetable.modules.service.finance.IBankFlowImportConfigService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BankFlowImportConfigServiceImpl extends ServiceImpl<BankFlowImportConfigMapper, BankFlowImportConfig> implements IBankFlowImportConfigService {
    
    @Override
    public IPage<BankFlowImportConfig> queryPage(BankFlowImportConfigQueryPO po) {
        // 转换Long类型为int类型用于分页
        int page = po.getPage() != null ? po.getPage().intValue() : 1;
        int limit = po.getLimit() != null ? po.getLimit().intValue() : 10;
        
        Page<BankFlowImportConfig> pageObj = new Page<>(page, limit);
        QueryWrapper<BankFlowImportConfig> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.like(po.getConfigName() != null && !po.getConfigName().isEmpty(), "config_name", po.getConfigName());
        queryWrapper.eq(po.getBankType() != null && !po.getBankType().isEmpty(), "bank_type", po.getBankType());
        queryWrapper.eq(po.getStatus() != null, "status", po.getStatus());
        
        queryWrapper.orderByDesc("create_time");
        return baseMapper.selectPage(pageObj, queryWrapper);
    }
    
    @Override
    public List<BankFlowImportConfig> queryAllEnabled() {
        QueryWrapper<BankFlowImportConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        queryWrapper.orderByDesc("create_time");
        return baseMapper.selectList(queryWrapper);
    }
    
    @Override
    public BankFlowImportConfig getByBankType(String bankType) {
        QueryWrapper<BankFlowImportConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bank_type", bankType);
        queryWrapper.eq("status", 0);
        return baseMapper.selectOne(queryWrapper);
    }
    
    @Override
    public boolean saveConfig(BankFlowImportConfig config) {
        return this.save(config);
    }
    
    @Override
    public boolean updateConfig(BankFlowImportConfig config) {
        return this.updateById(config);
    }
    
    @Override
    public boolean deleteConfig(Long id) {
        return this.removeById(id);
    }
}